package com.xxl.javawork;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/** 
 *计算器的窗口类和启动类
 * @author xiaxinlin
 * @version 1.0
 * @since jdk1.8
 * @date 2021/1/9 22:37
 */
public class Calculator extends JFrame implements ActionListener {

    private JPanel math;
    private JPanel rec;
    private JPanel cir;
    private JPanel tri;



    
    /**
     * //构造方法
     *
     * @author xiaxinlin
     * @creed: Talk is cheap,show me the code
     * @date 2021/1/9
     */
    public Calculator() {
        super("Caculator");//“超”关键字，表示调用父类的构造函数，
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("模式");
        JMenuItem item = new JMenuItem("数学计算器");
        JMenuItem item1 = new JMenuItem("矩形");
        JMenuItem item2 = new JMenuItem("圆形");
        JMenuItem item3 = new JMenuItem("三角形");
        item.addActionListener(this);
        item1.addActionListener(this);
        item2.addActionListener(this);
        item3.addActionListener(this);
        menu.add(item);
        menu.add(item1);
        menu.add(item2);
        menu.add(item3);
        menuBar.add(menu);
        this.setJMenuBar(menuBar);

        math = new MathCalculator();
        rec = new RecCalculator();
        cir = new CircleCalculator();
        tri = new TriCalaulator();

        this.add(rec);
        rec.setVisible(false);
        this.add(math);
        cir.setVisible(false);
        this.add(cir);
        this.add(tri);
        tri.setVisible(false);

        this.setLayout(null);
        this.setResizable(false);
        this.setBounds(500, 200, 567, 480);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    /**
     * //实现菜单点击时间方法
     *
     * @param e 
     * @return void
     * @author xiaxinlin
     * @creed: Talk is cheap,show me the code
     * @date 2021/1/9
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("数学计算器")){
            if (!math.isVisible()){
                closeAllPanel();
                math.setVisible(true);
            }
        }else if (e.getActionCommand().equals("矩形")){
            if (!rec.isVisible()){
                closeAllPanel();
                rec.setVisible(true);
            }
        }else if(e.getActionCommand().equals("圆形")){
            if (!cir.isVisible()){
                closeAllPanel();
                cir.setVisible(true);
            }
        }else if(e.getActionCommand().equals("三角形")){
            if (!tri.isVisible()){
                closeAllPanel();
                tri.setVisible(true);
            }
        }
    }

    /**
     *
     *设置所有计算模式的JPanel为不可见
     * @return void
     * @author xiaxinlin
     * @creed: Talk is cheap,show me the code
     * @date 2021/1/9
     */
    public void closeAllPanel(){
        math.setVisible(false);
        rec.setVisible(false);
        cir.setVisible(false);
        tri.setVisible(false);
    }

    /**
     * 计算器启动入口
     *
     * @param args 
     * @return void
     * @author xiaxinlin
     * @creed: Talk is cheap,show me the code
     * @date 2021/1/9
     */
    public static void main(String[] args) {
        new Calculator();
    }
}
